package com.yuexunit.operationman.nbsy.module.OperationInventory.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.OperationInventory.model.impl.InventoryModel;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.IInventoryPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.impl.InventoryPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.ui.view.IInventoryView;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/14 0014.
 */

public class Act_OperationInventory_SignForm extends BaseKJActivity implements ISafeOperationSignView, IInventoryView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.img_oi_sign_01)
    ImageView imgOiSign01;
    @Bind(R.id.btn_oi_sign_01)
    TextView btnOiSign01;
    @Bind(R.id.tv_oi_sign_01_time)
    TextView tvOiSign01Time;
    @Bind(R.id.tv_oi_sign_01_name)
    TextView tvOiSign01Name;
    @Bind(R.id.img_oi_sign_02)
    ImageView imgOiSign02;
    @Bind(R.id.btn_oi_sign_02)
    TextView btnOiSign02;
    @Bind(R.id.tv_oi_sign_02_time)
    TextView tvOiSign02Time;
    @Bind(R.id.tv_oi_sign_02_name)
    TextView tvOiSign02Name;

    LoginDialog loginDialog;
    LoadingDialog loadingDialog;
    private int type = -1;
    IInventoryPresenter presenter;
    ISafeOperationSignPresenter presenter_sign;

    private int contentType = -1;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_operationinventory_signform);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(aty);
        presenter = new InventoryPresenter(this, new InventoryModel());
        presenter_sign = new SafeOperationSignPresenter(this, new SafeOperationSignModel());
        contentType = getIntent().getExtras().getInt("type");
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientName());
        if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.INVENTORYBEAN.getObject().getCheck().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent() {
        switch (contentType) {
            case 1:
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getSignInstrumentBeforeTimeStr() != null) {
                    imgOiSign01.setVisibility(View.VISIBLE);
                    btnOiSign01.setVisibility(View.GONE);
                    tvOiSign01Time.setVisibility(View.VISIBLE);
                    tvOiSign01Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getSignInstrumentBeforeUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign01);
                    tvOiSign01Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getSignInstrumentBeforeTimeStr());
                    tvOiSign01Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getSignInstrumentBefore());
                }
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getSignCircurtBeforeTimeStr() != null) {
                    imgOiSign02.setVisibility(View.VISIBLE);
                    btnOiSign02.setVisibility(View.GONE);
                    tvOiSign02Time.setVisibility(View.VISIBLE);
                    tvOiSign02Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getSignCircurtBeforeUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign02);
                    tvOiSign02Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getSignCircurtBeforeTimeStr());
                    tvOiSign02Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getSignCircurtBefore());
                }
                break;
            case 2:
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBeforeTimeStr() != null) {
                    imgOiSign01.setVisibility(View.VISIBLE);
                    btnOiSign01.setVisibility(View.GONE);
                    tvOiSign01Time.setVisibility(View.VISIBLE);
                    tvOiSign01Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBeforeUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign01);
                    tvOiSign01Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBeforeTimeStr());
                    tvOiSign01Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBefore());
                }
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBefore2TimeStr() != null) {
                    imgOiSign02.setVisibility(View.VISIBLE);
                    btnOiSign02.setVisibility(View.GONE);
                    tvOiSign02Time.setVisibility(View.VISIBLE);
                    tvOiSign02Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBefore2Url())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign02);
                    tvOiSign02Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBefore2TimeStr());
                    tvOiSign02Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseBefore2());
                }
                break;
            case 3:
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfterTimeStr() != null) {
                    imgOiSign01.setVisibility(View.VISIBLE);
                    btnOiSign01.setVisibility(View.GONE);
                    tvOiSign01Time.setVisibility(View.VISIBLE);
                    tvOiSign01Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfterUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign01);
                    tvOiSign01Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfterTimeStr());
                    tvOiSign01Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfter());
                }
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfter2TimeStr() != null) {
                    imgOiSign02.setVisibility(View.VISIBLE);
                    btnOiSign02.setVisibility(View.GONE);
                    tvOiSign02Time.setVisibility(View.VISIBLE);
                    tvOiSign02Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfter2Url())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign02);
                    tvOiSign02Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfter2TimeStr());
                    tvOiSign02Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getBodyCloseAfter2());
                }
                break;
            case 4:
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfterTimeStr() != null) {
                    imgOiSign01.setVisibility(View.VISIBLE);
                    btnOiSign01.setVisibility(View.GONE);
                    tvOiSign01Time.setVisibility(View.VISIBLE);
                    tvOiSign01Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfterUrl())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign01);
                    tvOiSign01Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfterTimeStr());
                    tvOiSign01Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfter());
                }
                if (AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfter2TimeStr() != null) {
                    imgOiSign02.setVisibility(View.VISIBLE);
                    btnOiSign02.setVisibility(View.GONE);
                    tvOiSign02Time.setVisibility(View.VISIBLE);
                    tvOiSign02Name.setVisibility(View.VISIBLE);
                    Glide.with(aty)
                            .load(AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfter2Url())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgOiSign02);
                    tvOiSign02Time.setText("签名时间:" + AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfter2TimeStr());
                    tvOiSign02Name.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getSkinCloseAfter2());
                }
                break;
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.btn_oi_sign_01, R.id.btn_oi_sign_02})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.btn_oi_sign_01:
                showTypeDialog();
                type = 1;
                break;
            case R.id.btn_oi_sign_02:
                showTypeDialog();
                type = 2;
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    @Override
    public void insertInventoryDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateInventoryDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if (signBean.getCode() == 200) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            switch (type) {
                case 1:
                    switch (contentType) {
                        case 1:
                            imgOiSign01.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgOiSign01);
                            btnOiSign01.setVisibility(View.GONE);
                            tvOiSign01Time.setVisibility(View.VISIBLE);
                            tvOiSign01Time.setText("签名时间:" + str);
                            tvOiSign01Name.setVisibility(View.VISIBLE);
                            tvOiSign01Name.setText(signBean.getObject().getDoctorName());

                            AppConfig.INVENTORYBEAN.getObject().getCheck().setSignInstrumentBefore(signBean.getObject().getDoctorName());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setSignInstrumentBeforeUrl(signBean.getObject().getSignPicture());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setSignInstrumentBeforeTimeStr(str);
                            break;
                        case 2:
                            imgOiSign01.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgOiSign01);
                            btnOiSign01.setVisibility(View.GONE);
                            tvOiSign01Time.setVisibility(View.VISIBLE);
                            tvOiSign01Time.setText("签名时间:" + str);
                            tvOiSign01Name.setVisibility(View.VISIBLE);
                            tvOiSign01Name.setText(signBean.getObject().getDoctorName());

                            AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseBefore(signBean.getObject().getDoctorName());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseBeforeUrl(signBean.getObject().getSignPicture());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseBeforeTimeStr(str);
                            break;
                        case 3:
                            imgOiSign01.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgOiSign01);
                            btnOiSign01.setVisibility(View.GONE);
                            tvOiSign01Time.setVisibility(View.VISIBLE);
                            tvOiSign01Time.setText("签名时间:" + str);
                            tvOiSign01Name.setVisibility(View.VISIBLE);
                            tvOiSign01Name.setText(signBean.getObject().getDoctorName());

                            AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseAfter(signBean.getObject().getDoctorName());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseAfterUrl(signBean.getObject().getSignPicture());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseAfterTimeStr(str);
                            break;
                        case 4:
                            imgOiSign01.setVisibility(View.VISIBLE);
                            Glide.with(this)
                                    .load(signBean.getObject().getSignPicture())
                                    .placeholder(R.color.white)
                                    .error(R.color.white)
                                    .into(imgOiSign01);
                            btnOiSign01.setVisibility(View.GONE);
                            tvOiSign01Time.setVisibility(View.VISIBLE);
                            tvOiSign01Time.setText("签名时间:" + str);
                            tvOiSign01Name.setVisibility(View.VISIBLE);
                            tvOiSign01Name.setText(signBean.getObject().getDoctorName());

                            AppConfig.INVENTORYBEAN.getObject().getCheck().setSkinCloseAfter(signBean.getObject().getDoctorName());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setSkinCloseAfterUrl(signBean.getObject().getSignPicture());
                            AppConfig.INVENTORYBEAN.getObject().getCheck().setSkinCloseAfterTimeStr(str);
                            break;
                    }
                    break;
                case 2:
                    if (signBean.getObject().getDoctorType().equals("H")) {
                        switch (contentType) {
                            case 1:
                                imgOiSign02.setVisibility(View.VISIBLE);
                                Glide.with(this)
                                        .load(signBean.getObject().getSignPicture())
                                        .placeholder(R.color.white)
                                        .error(R.color.white)
                                        .into(imgOiSign02);
                                btnOiSign02.setVisibility(View.GONE);
                                tvOiSign02Time.setVisibility(View.VISIBLE);
                                tvOiSign02Time.setText("签名时间:" + str);
                                tvOiSign02Name.setVisibility(View.VISIBLE);
                                tvOiSign02Name.setText(signBean.getObject().getDoctorName());

                                AppConfig.INVENTORYBEAN.getObject().getCheck().setSignCircurtBefore(signBean.getObject().getDoctorName());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setSignCircurtBeforeUrl(signBean.getObject().getSignPicture());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setSignCircurtBeforeTimeStr(str);
                                break;
                            case 2:
                                imgOiSign02.setVisibility(View.VISIBLE);
                                Glide.with(this)
                                        .load(signBean.getObject().getSignPicture())
                                        .placeholder(R.color.white)
                                        .error(R.color.white)
                                        .into(imgOiSign02);
                                btnOiSign02.setVisibility(View.GONE);
                                tvOiSign02Time.setVisibility(View.VISIBLE);
                                tvOiSign02Time.setText("签名时间:" + str);
                                tvOiSign02Name.setVisibility(View.VISIBLE);
                                tvOiSign02Name.setText(signBean.getObject().getDoctorName());

                                AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseBefore2(signBean.getObject().getDoctorName());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseBefore2Url(signBean.getObject().getSignPicture());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseBefore2TimeStr(str);
                                break;
                            case 3:
                                imgOiSign02.setVisibility(View.VISIBLE);
                                Glide.with(this)
                                        .load(signBean.getObject().getSignPicture())
                                        .placeholder(R.color.white)
                                        .error(R.color.white)
                                        .into(imgOiSign02);
                                btnOiSign02.setVisibility(View.GONE);
                                tvOiSign02Time.setVisibility(View.VISIBLE);
                                tvOiSign02Time.setText("签名时间:" + str);
                                tvOiSign02Name.setVisibility(View.VISIBLE);
                                tvOiSign02Name.setText(signBean.getObject().getDoctorName());

                                AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseAfter2(signBean.getObject().getDoctorName());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseAfter2Url(signBean.getObject().getSignPicture());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setBodyCloseAfter2TimeStr(str);
                                break;
                            case 4:
                                imgOiSign02.setVisibility(View.VISIBLE);
                                Glide.with(this)
                                        .load(signBean.getObject().getSignPicture())
                                        .placeholder(R.color.white)
                                        .error(R.color.white)
                                        .into(imgOiSign02);
                                btnOiSign02.setVisibility(View.GONE);
                                tvOiSign02Time.setVisibility(View.VISIBLE);
                                tvOiSign02Time.setText("签名时间:" + str);
                                tvOiSign02Name.setVisibility(View.VISIBLE);
                                tvOiSign02Name.setText(signBean.getObject().getDoctorName());

                                AppConfig.INVENTORYBEAN.getObject().getCheck().setSkinCloseAfter2(signBean.getObject().getDoctorName());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setSkinCloseAfter2Url(signBean.getObject().getSignPicture());
                                AppConfig.INVENTORYBEAN.getObject().getCheck().setSkinCloseAfter2TimeStr(str);
                                break;
                        }
                    } else {
                        ViewInject.toast("请使用护士账号进行签名");
                    }
                    break;
            }


            if (AppConfig.INVENTORYBEAN.getObject().getCheck().getCheckId() == null) {
                Map<String, String> params = new HashMap<>();
                params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.INVENTORYBEAN));
                presenter.insertInventoryDate(params);
            } else {
                Map<String, String> params = new HashMap<>();
                params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.INVENTORYBEAN));
                presenter.updateInventoryDate(params);
            }
        } else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
